package com.shuoyue.ycgk.ui.mine.vip;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.VipPrice;
import com.shuoyue.ycgk.entity.VipPurchased;
import com.shuoyue.ycgk.entity.VipType;
import com.shuoyue.ycgk.ui.mine.coupon.CouponVilidateActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyVipActivity extends BaseMvpActivity<VipContract.Presenter> implements VipContract.View, VipCheckContract.View {
    boolean isOpenDialog = false;

    public static void start(Context source, boolean isOpenDialog) {
        source.startActivity(new Intent(source, MyVipActivity.class)
                .putExtra("b", isOpenDialog));
    }


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    VipCheckContract.Presenter checkPresenter;

    IndexFragmentStateAdapter indexFragmentAdapter;
    List<BaseMvpFragment> fragmentMainItems;
    List<VipPurchased> vipPurchase;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_vip;
    }

    @Override
    protected void initView() {
        pageTitle.setText("会员中心");
    }

    @Override
    public void initData() {
        isOpenDialog = getIntent().getBooleanExtra("b", false);
        mPresenter = new VipContract.Presenter();
        mPresenter.attachView(this);
        checkPresenter = new VipCheckContract.Presenter();
        checkPresenter.attachView(this);
//        mPresenter.getOrderList();
        checkPresenter.getVipPurchased();
    }

    int index = 0;

    @Override
    public void setBaseType(List<VipType> mTypes) {

        String perName = SPUtils.getLastSelectMoudle(mContext);


        fragmentMainItems = new ArrayList<>();
        for (int i = 0; i < mTypes.size(); i++) {
            VipType topType = mTypes.get(i);
            if (!TextUtils.isEmpty(perName) && topType.getName().equals(perName)) {
                //找到相同的
                index = i;
                topType.setAutoOpen(isOpenDialog);
            }
            if (vipPurchase != null)
                for (VipPurchased vipPurchased : vipPurchase) {
                    if (vipPurchased.getSubjectTypeId() == topType.getId()) {
                        topType.setMyVilidate(vipPurchased.getValidDate());
                    }
                }
            fragmentMainItems.add(MyVipFragment.getInstance(topType));
        }
        indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
        viewPager.setCurrentItem(index);
    }

    @Override
    public void setPriceList(List<VipPrice> vipPrices) {

    }


    @OnClick({R.id.back, R.id.my_coupon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.my_coupon:
                startActivity(new Intent(mContext, CouponVilidateActivity.class));
//                startActivity(new Intent(mContext, CouponSelectActivity.class));
                break;
        }
    }

    @Override
    public void isBuy(boolean isBuy) {

    }

    @Override
    public void setPurchased(List<VipPurchased> purchased) {
        this.vipPurchase = purchased;
        mPresenter.getVipModel();
    }
}
